package com.example.order;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.logging.SocketHandler;

public class ShopActivity extends AppCompatActivity {
    List<ShopBean> shopBeanList;
    ListView mLvShop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);

        initView();
        initData();
    }

    private void initData() {
        // 获取数据，封装List<ShopBean>
        shopBeanList = getInfoFromJson();
        ShopAdapter adapter = new ShopAdapter();
        mLvShop.setAdapter(adapter);

    }

    private List<ShopBean> getInfoFromJson() {
        try {
            InputStream is = getResources().getAssets().open("shop_list_data.json");
            String json = readAll(is); // json 字符串
            Gson gson = new Gson();
            Type type = new TypeToken<List<ShopBean>>(){}.getType();
            List<ShopBean> spl = gson.fromJson(json, type);
            return spl;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    // 从is 所有的字节读出来，转换成字符串
    private String readAll(InputStream is) {
        StringBuffer sb = new StringBuffer();
        try {

            int available = is.available();
            byte[] bytes = new byte[available];
            is.read(bytes);
            String s = new String(bytes);
            return s;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    private void initView() {
        mLvShop = findViewById(R.id.lv_shop);

    }
    class ShopAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return shopBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return shopBeanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(ShopActivity.this,R.layout.shop_item,null);
            ShopBean shopBean = shopBeanList.get(position);// 获取数据
            ImageView ivShopPic = view.findViewById(R.id.iv_shop_pic);
            ivShopPic.setImageResource(getImageResourceID(shopBean.getShopPic()));
            TextView tvShopName = view.findViewById(R.id.tv_shop_name);
            tvShopName.setText(shopBean.getShopName());
            return view;
        }

        /**
         * 根据文件名，获取资源ID
         * @param shopPic
         * @return
         */
        private int getImageResourceID(String shopPic) {
            int id = getResources().getIdentifier(shopPic, "mipmap", getPackageName());
            return id;
        }
    }
}